<%doc>
USAGE DOCUMENTATION :: PLEASE LEAVE THIS AT THE TOP OF THIS FILE

For Mako templating syntax documentation please visit: http://docs.makotemplates.org/en/latest/

Filename:           update_metadata.html
Version:            0.1
Variable names:     query [list]

query :: Usable parameters

== Global keys ==
query_string            Returns the string used for the search query.
title                   Returns the name of the movie, episode, or track.
parent_title            Returns the name of the album.
grandparent_title       Returns the name of the show, or artist.
media_index             Returns the index number of the episode, or track.
parent_media_index      Returns the index number of the season.
year                    Returns the release year of the movie, or show.
media_type              Returns the type of media. Either 'movie', 'show', 'season', 'episode', 'artist', 'album', or 'track'.
rating_key              Returns the unique identifier for the media item.

DOCUMENTATION :: END
</%doc>

<%inherit file="base.html"/>

<%def name="headIncludes()">
</%def>

<%def name="body()">
% if query:
<div class="container-fluid">
    <div class="row">
        <div class="summary-container">
            <div class="summary-navbar">
                <div class="col-md-12">
                    <div class="summary-navbar-list">
                        <ul class="list-unstyled breadcrumb">
                            % if query['media_type'] == 'movie':
                            <li>Movies</li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            <li class="active">${query['title']}</li>
                            % elif query['media_type'] == 'show':
                            <li>TV Shows</li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            <li class="active">${query['grandparent_title']}</li>
                            % elif query['media_type'] == 'season':
                            <li class="hidden-xs hidden-sm">TV Shows</li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            <li class="hidden-xs hidden-sm">${query['grandparent_title']}</li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            <li class="active">${query['parent_title']}</li>
                            % elif query['media_type'] == 'episode':
                            <li class="hidden-xs hidden-sm">TV Shows</li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            <li class="hidden-xs hidden-sm">${query['grandparent_title']}</li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            <li>${query['parent_title']}</li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            <li class="active">Episode ${query['media_index']} - ${query['title']}</li>
                            % elif query['media_type'] == 'artist':
                            <li>Music</li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            <li class="active">${query['grandparent_title']}</li>
                            % elif query['media_type'] == 'album':
                            <li class="hidden-xs hidden-sm">Music</li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            <li>${query['grandparent_title']}</li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            <li class="active">${query['parent_title']}</li>
                            % elif query['media_type'] == 'track':
                            <li class="hidden-xs hidden-sm">Music</li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            <li class="hidden-xs hidden-sm">${query['grandparent_title']}</li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            <li>${query['parent_title']}</li>
                            <span class="breadcrumb-arrow"><i class="fa fa-chevron-right"></i></span>
                            <li class="active">Track ${query['media_index']} - ${query['title']}</li>
                            % endif
                        </ul>
                    </div>
                </div>
            </div>
            <div class="summary-content-wrapper">
                <div class='col-md-12'>
                    % if not update:
                    <div style="text-align: center; margin-top: 20px;">
                        <i class="fa fa-exclamation-triangle"></i> Tautulli cannot find this item's metadata. Either this media item is not available in the Plex Media Server library or it has been moved.
                    </div>
                    % endif
                    <div style="text-align: center; margin-top: 20px;">
                        <i class="fa fa-info-circle"></i> Please select the correct match below to update the Tautulli database.
                    </div>
                    <div class='table-card-header'>
                        <div class="header-bar">
                            <span>Search Results for <strong><span id="update_query_title">${query['query_string']}</span></strong></span>
                        </div>
                        <div class="button-bar">
                            <form class="form" id="update_search_form">
                                    Incorrect search results? Try a different query:
                                    <div class="input-group" role="group" aria-label="Update Metadata Query">
                                        <input type="search" class="form-control" name="update_query" id="update_query" aria-label="Query" placeholder="Query..." value="${query['query_string']}" />
                                        <span class="input-group-btn">
                                            <button class="btn btn-form btn-dark btn-inactive" type="submit" id="update_search_button"><i class="fa fa-search"></i></button>
                                        </span>
                                    </div>
                            </form>
                        </div>
                    </div>
                    <div class='table-card-back'>
                        <div id="search-results-list" class="children-list"><i class="fa fa-refresh fa-spin"></i>&nbsp; Loading search results...</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
% else:
<div class="container-fluid">
    <div class="row">
        <div class="summary-container">
            <div class="summary-navbar">
                <div class="col-md-12">
                    <div class="summary-navbar-list">
                    </div>
                </div>
            </div>
            <div class="summary-content-wrapper">
                <div class='col-md-12'>
                    <div style="text-align: center; margin-top: 20px;">
                        <i class="fa fa-exclamation-triangle"></i> Tautulli cannot find this item's metadata. Either this media item is not available in the Plex Media Server library or it has been moved.
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
% endif
</%def>

<%def name="modalIncludes()">
% if query:
<%
    from plexpy.helpers import short_season
%>
<div class="modal fade" id="confirm-modal-update" tabindex="-1" role="dialog" aria-labelledby="confirm-modal-update">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-remove"></i></button>
                <h4 class="modal-title" id="myModalLabel">Confirm Update</h4>
            </div>
            <div class="modal-body" style="text-align: center;">
                <p>Are you REALLY sure you want to replace
                <p>
                    <strong>
                        % if query['media_type'] == 'movie':
                        ${query['title']}<br />${query['year']}
                        % elif query['media_type'] == 'show':
                        ${query['grandparent_title']}
                        % elif query['media_type'] == 'season':
                        ${query['grandparent_title']}<br />${query['parent_title']}
                        % elif query['media_type'] == 'episode':
                        ${query['grandparent_title']}<br />${query['title']}<br />${short_season(query['parent_title'])} &middot; E${query['media_index']}
                        % elif query['media_type'] == 'artist':
                        ${query['grandparent_title']}
                        % elif query['media_type'] == 'album':
                        ${query['grandparent_title']}<br />${query['parent_title']}
                        % elif query['media_type'] == 'track':
                        ${query['grandparent_title']}<br />${query['title']}<br />${query['parent_title']}
                        % endif
                    </strong>
                </p>
                <p> with </p>
                <p><span id="new_title"></span></p>
                <p>from the <strong><span id="new_library"></span></strong> library?</p>
                % if query['media_type'] != 'movie':
                <p>All items for <strong>${query['grandparent_title']}</strong> will also be updated.</p>
                % endif
                <p>This is permanent and cannot be undone!</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-dark" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-danger btn-ok" data-dismiss="modal" id="confirm-update">Update</button>
            </div>
        </div>
    </div>
</div>
% endif
</%def>

<%def name="javascriptIncludes()">

% if query:
<script>
    function getSearchResults() {
        var query_string = $('#update_query').val();
        $.ajax({
            url: 'get_search_results_children',
            type: 'GET',
            async: true,
            data: {
                query: query_string,
                limit: 30,
                media_type: '${query["media_type"]}',
                season_index: '${query["parent_media_index"]}'
            },
            complete: function (xhr, status) {
                $('#search-results-list').html(xhr.responseText);
                $('#update_query_title').text(query_string)
            }
        });
    }

    $(document).on('click', '#search-results-list a', function (e) {
        e.preventDefault();
        var new_rating_key = $(this).data('rating_key');
        var new_library_section = $(this).data('library_name');
        var new_href = $(this).attr('href');

        $('#new_title').html($(this).find('.item-children-instance-text-wrapper').html());
        $('#new_library').text(new_library_section);

        $('#confirm-modal-update').modal();
        $('#confirm-modal-update').one('click', '#confirm-update', function () {
            $(this).prop('disabled', true);
            var msg = '<i class="fa fa-refresh fa-spin"></i>&nbsp; Updating database...';
            showMsg(msg, false, false, 0);

            $.ajax({
                url: 'update_metadata_details',
                data: {
                    old_rating_key: '${query["rating_key"]}',
                    new_rating_key: new_rating_key,
                    media_type: '${query["media_type"]}',
                    single_update: true
                },
                async: true,
                success: function (data) {
                    window.location.href = new_href;
                }
            });
        });
    });

    getSearchResults();
    $('#update_search_form').submit(function (e) {
        e.preventDefault();
        if ($('#update_query').val().trim() != '')
        {
            getSearchResults();
        }
    })
</script>
% endif
</%def>